﻿using System;
using System.Data.SQLite;
using System.Diagnostics;
using System.Reflection;

namespace AddrDBContext
{
    public class DBCom
    {
        private static string DBFilePath = @"D:\DBFile\StandardAddress.db";

        /// <summary>
        /// 获取代码调用的模块名称
        /// </summary>
        /// <returns>模块名称</returns>
        public static string GetCallModuleName()
        {
            StackFrame frame = new StackFrame(2);
            MethodBase method = frame.GetMethod();
            return method.Module.Name;
        }

        //---删除表数据
        public static void DeleteTableData(string tableName, bool resetSequence = false)
        {
            SQLiteConnection cn = new SQLiteConnection("data source=" + DBFilePath);
            if (cn.State != System.Data.ConnectionState.Open)
            {
                cn.Open();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = cn;
                cmd.CommandText = string.Format("delete from {0}", tableName);
                cmd.ExecuteNonQuery();

                if(resetSequence)
                {
                    SQLiteCommand cmd0 = new SQLiteCommand();
                    cmd0.Connection = cn;
                    cmd0.CommandText = string.Format("update sqlite_sequence set seq=0 where name='{0}'", tableName);
                    cmd0.ExecuteNonQuery();
                }            
            }
            cn.Close();
        }

        public static Process FindTheProcess(string processName, int pid)
        {
            Process[] prc = Process.GetProcesses();
            foreach (Process pr in prc) //遍历整个进程
            {
                if (pr.ProcessName.Equals(processName) && pr.Id == pid && !pr.HasExited) //如果进程存在
                {
                    return pr;
                }
            }
            return null;
        }

        private static float GetWorkSet64(Process pro)
        {
            PerformanceCounter pf1 = new PerformanceCounter("Process", "Working Set - Private", pro.ProcessName);
            float size = pf1.NextValue() / 1024 / 1024;
            pf1.Dispose();
            return size;
        }

        public static T ParseDataFromDBReader<T>(SQLiteDataReader reader) where T : new()
        {
            T t = new T();
            PropertyInfo[] properties = t.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                if (reader.IsDBNull(reader.GetOrdinal(p.Name)))
                {
                    continue;
                }
                try
                {
                    if (p.PropertyType == typeof(string))
                    {
                        string value = reader.GetString(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                    else if (p.PropertyType == typeof(char))
                    {
                        char value = reader.GetChar(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                    else if (p.PropertyType == typeof(bool) || p.PropertyType == typeof(System.Boolean))
                    {
                        bool value = reader.GetBoolean(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                    else if (p.PropertyType == typeof(int))
                    {
                        int value = reader.GetInt32(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                    else if (p.PropertyType == typeof(DateTime))
                    {
                        DateTime value = reader.GetDateTime(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                    else
                    {
                        string value = reader.GetString(reader.GetOrdinal(p.Name));
                        p.SetValue(t, value, null);
                    }
                }
                catch (Exception ex)
                {
                    string error = string.Format("对象{0}的列名{1}值转换无效.", t.GetType().Name, p.Name);
                    Console.WriteLine(error);
                    Console.WriteLine(ex.ToString());
                }
            }         
            return t;                     
        }
    }
}
